package com.air.ota.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;
import com.air.ota.mo.DepositTrans;
import com.air.ota.service.IDepositTransService;
import com.air.ota.service.IEpayService;
import com.air.ota.service.ILastDepositTransHandlerService;
import com.air.ota.util.AppConstant;
import com.air.ota.util.HexStringUtil;
import com.air.ota.util.SZTInterface;
import com.air.ota.vo.LastTransDataVO;
import com.air.ota.vo.SZTBaseResponseVO;
import com.air.ota.vo.SZTDepositResultReqTransVO;
import com.air.ota.vo.SZTDepositResultRequestVO;
import com.air.ota.vo.SZTDepositResultResTransVO;

@Service("lastDepositTransHandlerService")
public class LastDepositTransHandlerServiceImp extends BaseUplinkMsgHandler 
	implements	ILastDepositTransHandlerService {
	
	@Autowired
	IDepositTransService depositTransService;
	@Autowired
	IEpayService epayService;
	
	@Override
	public boolean process(byte[] lastTransData,String cellphone) throws Exception {
		System.out.println("lastTransData1="+HexStringUtil.bytesToHexString(lastTransData));
		int transLength = 14;
		int transCount = lastTransData.length/transLength;
		boolean result = true;
		for(int i=0;i<transCount;i++){
			byte[] oneTransData = new byte[14];
			
			System.arraycopy(lastTransData, i*transLength, oneTransData, 0, transLength);
			boolean aResult=this.processOneTrans(oneTransData,cellphone);
			if (!aResult){
				result = false;
			}
			
		}
		return result;
	}
	
	private boolean processOneTrans(byte[] transData,String cellphone) throws Exception{
		System.out.println("transData2="+HexStringUtil.bytesToHexString(transData));
		LastTransDataVO lastTrans = new LastTransDataVO(transData);
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("depositSequence", HexStringUtil.bytesToHexString(lastTrans.getDepositSequence()), QueryExpression.EQUALS);
		condition.addQueryCondition("cellphone", cellphone, QueryExpression.EQUALS);
		//condition.addQueryCondition("ewalletRemain", HexStringUtil.bytesToHexString(lastTrans.getRemain()), QueryExpression.EQUALS);
		List<DepositTrans> result=depositTransService.queryByCondition(condition);
		if (result==null||result.size()==0){
			return false;
		}
		DepositTrans trans = result.get(0);
		
		System.out.println("lastTrans result="+HexStringUtil.bytesToHexString(lastTrans.getResult())+" depositSequence="+HexStringUtil.bytesToHexString(lastTrans.getDepositSequence()));
		System.out.println("lastTransID Found="+trans.getId());
		if (HexStringUtil.bytesToHexString(lastTrans.getResult()).equals("9000")){//UTK成功
			if (trans.getResult()!=AppConstant._depositResult_sztResult_success && trans.getResult()>0){//UTK成功，SZT不成功
				trans.setResult(AppConstant._depositResult_utkResult_success);
				trans.setTac(HexStringUtil.bytesToHexString(lastTrans.getTac()));
				trans.setEwalletRemain(HexStringUtil.bytesToHexString(lastTrans.getRemain()));
				trans.setCardTransSequence(HexStringUtil.bytesToHexString(lastTrans.getOnlineTransSequence()));
				depositTransService.updateById(trans);
				unlockRetryOutboundMsg(trans.getId(),AppConstant._outbound_msg_type_depositResponse);
				
				byte[] deviceSequence = HexStringUtil.hexStringToBytes(trans.getDeviceSequence());
				byte[] sztSequence = HexStringUtil.hexStringToBytes(trans.getSztId());
				byte[] cardAppSequence = HexStringUtil.hexStringToBytes(trans.getCardAppSequence());
				byte[] depositResult = HexStringUtil.hexStringToBytes("00000000");
				
				SZTDepositResultReqTransVO sztDepositResultReqTransVO = new SZTDepositResultReqTransVO(
								deviceSequence,sztSequence,trans.getDeviceTriggerTime(),
								HexStringUtil.hexStringToBytes(trans.getCardAppSequence()),
								depositResult,
								HexStringUtil.hexStringToBytes(trans.getTac()),
								HexStringUtil.hexStringToBytes(trans.getEwalletRemain()),
								HexStringUtil.hexStringToBytes(trans.getCardTransSequence())
							);
				
				SZTDepositResultRequestVO sztDepositResultRequestVO = new SZTDepositResultRequestVO(sztDepositResultReqTransVO.getTransDataHexString());
				SZTInterface sztInterface = new SZTInterface();
				byte[] response=sztInterface.sendRequest(HexStringUtil.hexStringToBytes(sztDepositResultRequestVO.getMsgPackage()));
				SZTBaseResponseVO responseVO = new SZTBaseResponseVO();
				responseVO.parse(response);
				
				SZTDepositResultResTransVO depositResultResTransVO = new SZTDepositResultResTransVO();
				depositResultResTransVO.parse(HexStringUtil.hexStringToBytes(responseVO.getData()));
				
				if (depositResultResTransVO.getTransResult()==0x00){//Success
					System.out.println("success");
					trans.setResult(AppConstant._depositResult_sztResult_success);
					depositTransService.updateById(trans);
				}else{
					System.out.println("failure code="+HexStringUtil.byteToHexString(depositResultResTransVO.getTransResult()));
					trans.setResult(AppConstant._depositResult_sztResult_failure);
					depositTransService.updateById(trans);
					return true;
				}
				
			}
			
			
		}else{//UTK不成功
			//if >0 refund
			if (trans.getResult()>0){
				boolean epayRefundResult = epayService.refund(trans);
				if (epayRefundResult){
					trans.setResult(AppConstant._depositResult_refund_success);
				}else{
					trans.setResult(AppConstant._depositResult_refund_failure);
				}
				depositTransService.updateById(trans);
			}
			
			
		}
		
		
		return true;
		
		
		//if trans.getResult() != 
		
		//boolean epayRefundResult = epayService.refund(trans);
		
		
	}
	
}
